<?php
/**
 * Description of 
 *
 * @author Marco Mounzer
 * @email m.mounzer@gmail.com
 * @copyright 2013
 */
require_once '../lib/begin.php';

if (!isset($_SESSION['user']) || get_class($_SESSION['user']) != 'WebUser')
    header('location: /');


$obj = new WebUser();
try {
    if (isset($_POST['_save'])) {
        $obj->setObjectFromFormSubmit($_POST);

        $obj = Database::SaveOrUpdateObject($obj);

        header('location: /admin/user.php');
        return;
    } else if (isset($_POST['_delete'])) {
        $obj->setObjectFromFormSubmit($_POST);
        if (Database::DeleteObject($obj)) {
            require_once '_header.php';
            echo "<br /><br />Elemento eliminato";
            require_once '_footer.php';
            die;
        }
    } else {
        $obj = Database::GetIstance()->loadObject($obj, $_SESSION['user']->Id);
    }
} catch (PDOException $e) {
    $err = '<b/>Si è verificato un errore: ' . $e->getMessage() . '<br />Error code: ' . $e->getCode() . '<br />Dati non salvati</b>';
} catch (Exception $e) {
    $err = '<b/>Si è verificato un errore: ' . $e->getMessage() . '<br />Error code: ' . $e->getCode() . '<br />Dati non salvati</b>';
}

require_once '_header.php';

if (isset($err))
    echo $err;

if ($obj === false) {
    echo "Nessun dato trovato.";
    require_once '_footer.php';
    die;
}

echo $obj->getFormHtml($obj->getForm());

require_once '_footer.php';